package org.example.config;

import lombok.extern.slf4j.Slf4j;
import org.example.interceptor.JwtTokenUserInterceptor;
import org.example.interceptor.MiniAppAuthInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@Slf4j
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Autowired
    private JwtTokenUserInterceptor jwtTokenUserInterceptor;
    
    @Autowired
    private MiniAppAuthInterceptor miniAppAuthInterceptor;


    /**
     * 注册自定义拦截器
     *
     * @param registry
     */
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册原有JWT拦截器（用于Web端）
        registry.addInterceptor(jwtTokenUserInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(
                    "/api/questionnaires/**",    // 排除问卷相关API
                    "/api/user-responses/**",    // 排除问卷回答相关API
                    "/api/article/**",           // 排除文章相关API
                    "/api/login/wechat",         // 排除微信登录API
                    "/api/UserInfo",             // 排除用户信息更新API
                    "/image/**",                 // 排除图片资源
                    "/doc.html",                 // Knife4j文档页面
                    "/webjars/**",               // Knife4j静态资源
                    "/swagger-resources/**",     // Swagger资源
                    "/v3/api-docs/**",           // OpenAPI 3.0文档
                    "/v2/api-docs/**",           // Swagger 2.0文档
                    "/favicon.ico"               // 网站图标
                );
                
        // 注册小程序专用拦截器
        registry.addInterceptor(miniAppAuthInterceptor)
                .addPathPatterns(
                    "/api/user-responses/**",     // 问卷回答需要身份验证
                    "/api/UserInfo"               // 用户信息更新需要身份验证
                )
                .excludePathPatterns(
                    "/api/login/wechat",          // 排除微信登录API
                    "/api/questionnaires/**",     // 公开的问卷API
                    "/api/article/**",            // 公开的文章API
                    "/image/**",                  // 排除图片资源
                    "/doc.html",                  // 排除文档
                    "/webjars/**",
                    "/swagger-resources/**",
                    "/v3/api-docs/**",
                    "/v2/api-docs/**",
                    "/favicon.ico"
                );
    }

    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 配置问题图片路径
        registry.addResourceHandler("/image/question/**")
                .addResourceLocations("classpath:/static/image/question/", "file:./static/image/question/", "file:./image/question/")
                .setCachePeriod(3600);
        
        // 配置文章图片路径
        registry.addResourceHandler("/image/article/**")
                .addResourceLocations("classpath:/static/image/article/", "file:./static/image/article/", "file:./image/article/")
                .setCachePeriod(3600);
                
        // 保留原始配置作为默认路径
        registry.addResourceHandler("/image/**")
                .addResourceLocations("classpath:/static/image/", "file:./static/image/", "file:./image/")
                .setCachePeriod(3600);
    }
}
